package com.grumoon.volleydemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.grumoon.volleydemo.R;

/**
 * 
 ***************************************************** 
 * <hr>
 * <dt><span class="strong">类功能简介:</span></dt>
 * <dd>Coffee</dd>
 * <dt><span class="strong">创建时间:</span></dt>
 * <dd>2015-3-19 下午2:56:12</dd>
 * <dt><span class="strong">公司:</span></dt>
 * <dd>Image请求 基础Adapter</dd>
 * 
 * @author aa1000777 - Email:aa1000777@qq.com
 ***************************************************** 
 */
public abstract class ImageBaseAdapter extends BaseAdapter {
	private String[] imageUrlArray;
	private LayoutInflater inflater;

	public ImageBaseAdapter(Context context, String[] imageUrlArray) {
		this.imageUrlArray = imageUrlArray;
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		if (imageUrlArray == null) {
			return 0;
		} else {
			return imageUrlArray.length;
		}
	}

	@Override
	public Object getItem(int position) {
		if (imageUrlArray == null || position >= imageUrlArray.length) {
			return null;
		} else {
			return imageUrlArray[position];
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if (convertView == null) {
			convertView = this.inflater.inflate(getItemLayout(), parent, false);
			viewHolder = new ViewHolder();
			viewHolder.ivCar = (ImageView) convertView.findViewById(R.id.iv_car);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		setImage(viewHolder.ivCar, imageUrlArray[position]);
		return convertView;
	}

	static class ViewHolder {
		ImageView ivCar;
	}

	abstract int getItemLayout();

	abstract void setImage(ImageView imageView, String imageUrl);

}
